﻿using DataAchive.Helper;
using DataAchive.Model;
using DataAchive.Model.Excel.Export;
using DataAchive.Model.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using System.IO;
using System.Linq;
using System.Windows;

namespace DataAchive
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class SiteInfoWindow : Window
    {
        public SiteInfoWindow()
        {
            InitializeComponent();
            SiteViewModel siteViewModel = new SiteViewModel();
            this.DataContext = siteViewModel;
            BingData(new AChiveSite());
        }


        public void BingData(AChiveSite queryEntity)
        {
            M_QueryBuilder queryBuilder = new M_QueryBuilder();
            List<AChiveSite> list = queryBuilder.GetModelList(queryEntity);
            List<SiteViewModel.SiteData> resultData = list.Select(x => new SiteViewModel.SiteData()
            {
                Id = Convert.ToInt32(x.Id ?? 0),
                Line_Id = x.Line_Id,
                Equipment_Id = x.Equipment_Id,
                IP = x.IP,
                Row_Number = x.Row_Number,
                Row_Order = x.Row_Order,
                Row_Type = x.Row_Type,
                Colum_Number = x.Colum_Number,
                Colum_Order = x.Colum_Order,
                Colum_Type = x.Colum_Type,
                Top_Bottom_Margin = x.Top_Bottom_Margin,
                Left_Right_Margin = x.Left_Right_Margin,
            }).ToList();
            SiteViewModel siteViewModel = this.DataContext as SiteViewModel;
            siteViewModel.AllData = new ObservableCollection<SiteViewModel.SiteData>(resultData);
            siteViewModel.SiteQuery = new SiteViewModel.SiteData();
        }

        private void Select_Click(object sender, RoutedEventArgs e)
        {
            SiteViewModel siteViewModel = this.DataContext as SiteViewModel;
            AChiveSite queryEntity = new AChiveSite();
            queryEntity.Line_Id = siteViewModel.SiteQuery.Line_Id;
            queryEntity.Equipment_Id = siteViewModel.SiteQuery.Equipment_Id;
            BingData(queryEntity);
        }

        private void Exprot_Click(object sender, RoutedEventArgs e)
        {

            SiteViewModel siteViewModel = this.DataContext as SiteViewModel;
            // 检查文件夹是否存在，不存在则创建
            string path = System.Environment.CurrentDirectory + @"\ExcelFile";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string fileName = $@"{path}\Site-{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
            List<SiteExportViewModel> dataList = siteViewModel.AllData.Select(s => new SiteExportViewModel()
            {
                ID = s.Id,
                Line_Id = s.Line_Id,
                Equipment_Id = s.Equipment_Id,
                IP = s.IP,
                Row_Number = s.Row_Number,
                Row_Order = s.Row_Order,
                Row_Type = s.Row_Type,
                Colum_Number = s.Colum_Number,
                Colum_Order = s.Colum_Order,
                Colum_Type = s.Colum_Type,
                Top_Bottom_Margin = s.Top_Bottom_Margin,
                Left_Right_Margin = s.Left_Right_Margin
            }).ToList();

            ExcelHelper.Export(fileName, dataList);
        }




    }
}
